/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MyClass;

import java.util.Vector;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

/**
 *
 * @author HuuTai_it
 */
public class EnemyManagement {
    
    private GameManagement gameMng;
    private long time = System.currentTimeMillis();
    private Image blackShip = Image.createImage("/MyPicture/blackship.png");
    private Image redShip = Image.createImage("/MyPicture/redship.png");
    private Image blueShip = Image.createImage("/MyPicture/blueship.png");
    private Image ghostShip = Image.createImage("/MyPicture/ghostship.png");
    private Image monster = Image.createImage("/MyPicture/monster.png");
    public Vector vEnemy = new Vector();
    public Ship  mons;
    
    public EnemyManagement(GameManagement gameMng) throws Exception {
                
        this.gameMng = gameMng;
        int numBlackShip = 0;
        int numRedShip = 0;
        int numBlueShip = 0;
        int numGhostShip = 0;
        int numMonster = 0;
        mons = null;
        int lv = gameMng.control.level;
        int diff = gameMng.control.difficulties;

        numBlackShip = lv % 4 + 1 + diff + Math.abs(gameMng.r.nextInt() % 3);
        numRedShip = Math.abs(gameMng.r.nextInt() % 2) * lv / 5 +  lv / 10 + Math.abs(gameMng.r.nextInt() % 2) * diff * lv / 15;
        numBlueShip = Math.abs(gameMng.r.nextInt() % 2) * (lv / 5) +  lv / 10 + Math.abs(gameMng.r.nextInt() % 2) * diff / 3;
        numGhostShip = Math.abs(gameMng.r.nextInt() % 2) * (lv / 5) +  lv / 10 + Math.abs(gameMng.r.nextInt() % 2) * diff / 3;
        if(lv > 5)
            numMonster = Math.abs(gameMng.r.nextInt() % 2);
        if(numMonster == 1) 
            mons = new Monster(gameMng, monster) ;       
        for(int i = 0; i < numBlackShip; i++) {    
                
            Ship s = new BlackShip(gameMng, blackShip);
            vEnemy.addElement(s);
        }
        
        for(int i = 0; i < numRedShip; i++) {     
                
            Ship s = new RedShip(gameMng, redShip);
            vEnemy.addElement(s); 
        }
        
        for(int i = 0; i < numBlueShip; i++) {
                
            Ship s = new BlueShip(gameMng, blueShip);
            vEnemy.addElement(s);
        }
        
        for(int i = 0; i < numGhostShip; i++) {
                
            Ship s = new GhostShip(gameMng, ghostShip);
            vEnemy.addElement(s);
        }
       
    }
    
    public void wobble(long currTime) {
        try
        {
        boolean res = true;
        int b = 0;
        int n = vEnemy.size();
        if(currTime - time > 150)
        {
            if(mons != null) mons.nextFrame();
            for(int i = 0; i < n; i++)
            {
                if(((Sprite)vEnemy.elementAt(i)).getFrameSequenceLength() == 16)
                {
                    res = true;
                    if(((Sprite)vEnemy.elementAt(i)).getFrame() == 14)
                    {
                        res = false;
                        
                        
                        ((Sprite)vEnemy.elementAt(i)).setVisible(false);
                        gameMng.layerManager.remove(((Ship)vEnemy.elementAt(i)).a);
                        gameMng.layerManager.remove(((Ship)vEnemy.elementAt(i)).b);
                        gameMng.layerManager.remove(((Sprite)vEnemy.elementAt(i)));
                    }
                    else
                        ((Sprite)vEnemy.elementAt(i)).nextFrame();
                }
                else
                {
                    b++;
                    ((Sprite)vEnemy.elementAt(i)).nextFrame();
                }
            }//k co thuyen no
            if(b == n) 
                gameMng.burning = false;
            else
                gameMng.burning = res;
            time = currTime;
        }
        }
        catch(ArrayIndexOutOfBoundsException ex){
            ex.printStackTrace();
              System.out.println(ex);
        }
    }
        
}
